/**
 * @file cta_phonebook.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_CTA!= 0

/*********************
 *      DEFINES
 *********************/
#define CTA_PHONEBOOK_KEY_NUM  11

/**********************
 *      TYPEDEFS
 **********************/
typedef struct {
    lv_watch_obj_ext_t old_ext;
    lv_obj_t * ddlist_name;
    lv_obj_t * ta_number;
    uint8_t * portrait;
    uint8_t max_name_len;
} lv_cta_phonebook_add_ext_t;

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void cta_phonebook_prepare_destory(lv_obj_t * activity_obj);
static void cta_phonebook_add_event_cb(lv_obj_t * btn, lv_event_t e);
static void cta_phonebook_del_event_cb(lv_obj_t * btn, lv_event_t e);

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
lv_obj_t * cta_phonebook_main_create(lv_obj_t * activity_obj)
{
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_CTA_PHONEBOOK;
        activity_ext.create = cta_phonebook_main_create;
        activity_ext.prepare_destory = cta_phonebook_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
    }

    lv_obj_t * obj;
    obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);

    lv_obj_t * label_title = lv_label_create(obj, NULL);
    lv_obj_add_style(label_title, LV_LABEL_PART_MAIN, &lv_watch_font20_gray);
    lv_label_set_text_id(label_title, WATCH_TEXT_ID_CTA_PHONEBOOK);
    lv_coord_t label_interval = 10;
    lv_obj_align(label_title, obj, LV_ALIGN_IN_TOP_MID, 0, label_interval);

    lv_obj_t * list = lv_list_create(obj, NULL);
    lv_watch_obj_add_element(list);
    lv_watch_obj_add_element(lv_page_get_scrl(list));
    lv_coord_t interval = 20;
    lv_obj_set_size(list, LV_HOR_RES, LV_VER_RES - lv_obj_get_height(label_title) - interval - 10);
    lv_page_set_edge_flash(list, true);
    lv_obj_add_style(list, LV_PAGE_PART_EDGE_FLASH, &lv_watch_cont_opa1);
    lv_obj_align(list, label_title, LV_ALIGN_OUT_BOTTOM_MID, 0, interval);

    lv_factory_obj_ext_t * ext = factorymode_get_ext();
    if(NULL == ext) return NULL;
    lv_obj_add_style(list, LV_LIST_PART_BG,  &lv_watch_style_transp);

    lv_obj_t * btn = lv_list_add_btn(list, NULL, lv_lang_get_text(WATCH_TEXT_ID_ADD_CONTACT));
    lv_watch_obj_add_element(btn);
    lv_obj_add_style(btn, LV_BTN_PART_MAIN, &(ext->list_btn_st));
    lv_obj_set_event_cb(btn, cta_phonebook_add_event_cb);
    lv_obj_t * label_txt = lv_obj_get_child(btn, NULL);
    lv_label_set_align(label_txt, LV_LABEL_ALIGN_CENTER);
    lv_label_set_long_mode(label_txt, LV_LABEL_LONG_BREAK);
    lv_obj_set_width(label_txt, LV_HOR_RES - 10);

    btn = lv_list_add_btn(list, NULL, lv_lang_get_text(WATCH_TEXT_ID_DEL_ALL_CONTACT));
    lv_watch_obj_add_element(btn);
    lv_obj_add_style(btn, LV_BTN_PART_MAIN, &(ext->list_btn_st));
    lv_obj_set_event_cb(btn, cta_phonebook_del_event_cb);
    label_txt = lv_obj_get_child(btn, NULL);
    lv_label_set_align(label_txt, LV_LABEL_ALIGN_CENTER);
    lv_label_set_long_mode(label_txt, LV_LABEL_LONG_BREAK);
    lv_obj_set_width(label_txt, LV_HOR_RES - 10);

    return obj;
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static void cta_phonebook_prepare_destory(lv_obj_t * activity_obj)
{
    lv_watch_png_cache_all_free();
}

static void * cta_phonebook_get_ext(lv_watch_Activity_Id_t act_id)
{
    lv_obj_t * activity_obj = lv_watch_get_activity_obj(act_id);
    if(NULL == activity_obj) return NULL;

    lv_obj_t * obj = NULL;
    lv_watch_get_child_obj(activity_obj, lv_watch_obj_signal, &obj);
    if(NULL == obj) return NULL;

    if(obj) {
        void * ext = lv_obj_get_ext_attr(obj);
        return ext;
    }
    return NULL;
}

static void cta_phonebook_notice_anim(lv_anim_t * para)
{
    if(NULL == para) return;

    lv_obj_t * activity_obj = (lv_obj_t *)para->var;
    lv_obj_del(activity_obj);
}

static lv_obj_t * cta_phonebook_notice_create(watchLangTxtId_t text_id)
{
    lv_watch_activity_ext_t activity_ext;
    memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
    activity_ext.actId = ACT_ID_CTA_PHONEBOOK_NOTICE;
    activity_ext.create = NULL;
    activity_ext.prepare_destory = NULL;
    lv_obj_t * activity_obj = lv_watch_creat_activity_obj(&activity_ext);
    LV_ASSERT_MEM(activity_obj);

    lv_obj_t * obj;
    obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);

    lv_obj_t * label = lv_label_create(obj, NULL);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20);
    lv_label_set_text_id(label, text_id);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_label_set_long_mode(label, LV_LABEL_LONG_BREAK);
    lv_obj_set_width(label, LV_HOR_RES - 10);
    lv_obj_align(label, obj, LV_ALIGN_CENTER, 0, 0);

    lv_anim_path_t path;
    lv_anim_path_init(&path);
    lv_anim_path_set_cb(&path, lv_anim_path_step);

    lv_anim_t a;
    lv_anim_init(&a);
    lv_anim_set_var(&a, activity_obj);
    lv_anim_set_time(&a, 1000);
    lv_anim_set_ready_cb(&a, (lv_anim_ready_cb_t)cta_phonebook_notice_anim);
    lv_anim_set_path(&a, &path);
    lv_anim_start(&a);

    return obj;
}

static void cta_phonebook_save_event_cb(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        lv_cta_phonebook_add_ext_t * ext = cta_phonebook_get_ext(ACT_ID_CTA_PHONEBOOK_ADD);
        if(NULL == ext) {
            printf("%s: cat phonebook ext is null\n", __FUNCTION__);
            return;
        }

        const char * number = lv_textarea_get_text(ext->ta_number);
        uint8_t len_number = strlen(number);
        if(0 == len_number) {
            lv_obj_t * obj = cta_phonebook_notice_create(WATCH_TEXT_ID_NO_NUMBER);
            LV_ASSERT_MEM(obj);
            return;
        }

        char * name = (char *)lv_mem_alloc(ext->max_name_len);
        memset(name, 0, ext->max_name_len);
        lv_dropdown_get_selected_str(ext->ddlist_name, name, ext->max_name_len);

        uint32_t length = sizeof(nv_watch_phonebook_t);
        nv_watch_phonebook_t * nvm_contact_list = (nv_watch_phonebook_t *)lv_mem_alloc(length);
        if(length != UI_NV_Read_Req(NV_SECTION_UI_PHONEBOOK, 0, length, (uint8_t *)nvm_contact_list)) {
            printf("read nvm error in cta_phonebook_save\n");
            lv_mem_free(nvm_contact_list);
            lv_mem_free(name);
            return;
        }

        uint8_t i = 0;
        for(; i < NV_WATCH_MAX_CONTACT_NUM; i++) {
            if('\0' == nvm_contact_list->info[i].number[0]) {
                break;
            }

            if(0 == strcmp(name, nvm_contact_list->info[i].name)) {
                lv_mem_free(nvm_contact_list);
                lv_mem_free(name);
                lv_obj_t * obj = cta_phonebook_notice_create(WATCH_TEXT_ID_CONTACT_ALREADY_EXIST);
                LV_ASSERT_MEM(obj);
                return;
            }
        }

        if(NV_WATCH_MAX_CONTACT_NUM == i) {
            lv_mem_free(nvm_contact_list);
            lv_mem_free(name);
            lv_obj_t * obj = cta_phonebook_notice_create(WATCH_TEXT_ID_CONTACTS_FULL);
            LV_ASSERT_MEM(obj);
            return;
        }

        memcpy(nvm_contact_list->info[i].name, name, strlen(name) + 1);
        memcpy(nvm_contact_list->info[i].number, number, len_number + 1);
        if(0 == i) {
            nvm_contact_list->info[i].contact_type = 0; /*administrator*/
        } else {
            nvm_contact_list->info[i].contact_type = 2; /*others*/
        }
        uint8_t index = lv_dropdown_get_selected(ext->ddlist_name);
        nvm_contact_list->info[i].portrait_id = ext->portrait[index];
        printf("--------------------------------portrait id %d, index %d in cta_phonebook_save\n",
               nvm_contact_list->info[i].portrait_id, index);
        if(length != UI_NV_Write_Req(NV_SECTION_UI_PHONEBOOK, 0, length, (uint8_t *)nvm_contact_list)) {
            printf("write nvm error in cta_phonebook_save\n");
            lv_mem_free(nvm_contact_list);
            lv_mem_free(name);
            return;
        }
        lv_mem_free(nvm_contact_list);
        lv_mem_free(name);

        cta_phonebook_notice_create(WATCH_TEXT_ID_CONTACT_SAVED);

        lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_CTA_PHONEBOOK_ADD);
        if(NULL == activity_obj) {
            printf("%s: activity_obj is null\n", __FUNCTION__);
            return;
        }
        lv_watch_activity_ext_t * act_ext = lv_obj_get_ext_attr(activity_obj);
        if(NULL == act_ext) {
            printf("%s: act_ext is null\n", __FUNCTION__);
            return;
        }
        if(act_ext->prepare_destory) {
            act_ext->prepare_destory(activity_obj);
        }
        lv_obj_del(activity_obj);
    }
}

static uint8_t cta_phonebook_get_portrait_id(watchLangTxtId_t name_id)
{
    uint8_t portrait_id = WATCH_PORTRAIT_ID_FAMILY;
    switch(name_id) {
        case WATCH_TEXT_ID_BOY:
            portrait_id = WATCH_PORTRAIT_ID_BOY;
            break;
        case WATCH_TEXT_ID_GIRL:
            portrait_id = WATCH_PORTRAIT_ID_GIRL;
            break;
        case WATCH_TEXT_ID_FAMILY:
            portrait_id = WATCH_PORTRAIT_ID_FAMILY;
            break;
        case WATCH_TEXT_ID_MATERNAL_GRADMOTHER:
            portrait_id = WATCH_PORTRAIT_ID_MATERNAL_GRADMOTHER;
            break;
        case WATCH_TEXT_ID_MATERNAL_GRADFATHER:
            portrait_id = WATCH_PORTRAIT_ID_MATERNAL_GRADFATHER;
            break;
        case WATCH_TEXT_ID_PATERNAL_GRADMOTHER:
            portrait_id = WATCH_PORTRAIT_ID_PATERNAL_GRADMOTHER;
            break;
        case WATCH_TEXT_ID_PATERNAL_GRADFATHER:
            portrait_id = WATCH_PORTRAIT_ID_PATERNAL_GRADFATHER;
            break;
        case WATCH_TEXT_ID_FATHER:
            portrait_id = WATCH_PORTRAIT_ID_FATHER;
            break;
        case WATCH_TEXT_ID_MOTHER:
            portrait_id = WATCH_PORTRAIT_ID_MOTHER;
            break;
        case WATCH_TEXT_ID_AUNT:
            portrait_id = WATCH_PORTRAIT_ID_AUNT;
            break;
        case WATCH_TEXT_ID_UNCLE:
            portrait_id = WATCH_PORTRAIT_ID_UNCLE;
            break;
        default:
            break;
    }

    return portrait_id;
}

static void cta_phonebook_add_prepare_destory(lv_obj_t * activity_obj)
{
    lv_cta_phonebook_add_ext_t * ext = cta_phonebook_get_ext(ACT_ID_CTA_PHONEBOOK_ADD);
    if(NULL == ext) return;
    if(ext->portrait) lv_mem_free(ext->portrait);
    lv_watch_png_cache_all_free();
}

static void cta_phonebook_btn_event_cb(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        lv_cta_phonebook_add_ext_t * ext = cta_phonebook_get_ext(ACT_ID_CTA_PHONEBOOK_ADD);
        if(NULL == ext) {
            printf("%s: cta phonebook ext is null\n", __FUNCTION__);
        } else {
            char key = lv_obj_get_user_data(btn).user_num;
            if(('0' <= key) && ('9' >= key)) {
                lv_textarea_add_char(ext->ta_number, key);
            } else if('x' == key) {
                lv_textarea_del_char(ext->ta_number);
            }
        }
    }
}

static lv_obj_t * cta_phonebook_add_create(lv_obj_t * activity_obj)
{
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_CTA_PHONEBOOK_ADD;
        activity_ext.create = cta_phonebook_add_create;
        activity_ext.prepare_destory = cta_phonebook_add_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
    }

    lv_obj_t * obj;
    obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    lv_cta_phonebook_add_ext_t * ext = lv_obj_allocate_ext_attr(obj, sizeof(lv_cta_phonebook_add_ext_t));
    LV_ASSERT_MEM(ext);

    lv_obj_t * cont_name = lv_cont_create(obj, NULL);
    lv_cont_set_fit2(cont_name, LV_FIT_NONE, LV_FIT_TIGHT);
    lv_obj_set_width(cont_name, lv_obj_get_width(obj));
    lv_obj_add_style(cont_name, LV_CONT_PART_MAIN, &lv_watch_style_transp);
    lv_watch_obj_add_element(cont_name);

    lv_obj_t * label_name = lv_label_create(cont_name, NULL);
    lv_obj_add_style(label_name, LV_LABEL_PART_MAIN, &lv_watch_font20);
    lv_label_set_text_id(label_name, WATCH_TEXT_ID_NAME);
    lv_obj_align(label_name, cont_name, LV_ALIGN_IN_LEFT_MID, 20, 0);

    ext->ddlist_name = lv_dropdown_create(cont_name, NULL);
    lv_obj_set_style_local_text_font(ext->ddlist_name, LV_DROPDOWN_PART_MAIN, LV_STATE_DEFAULT, LV_THEME_WATCH_NIGHT_FONT_SMALL);
    lv_obj_set_style_local_bg_opa(ext->ddlist_name, LV_DROPDOWN_PART_SCROLLBAR, LV_STATE_DEFAULT, LV_OPA_0);

    /*add contact name id*/
    watchLangTxtId_t name_id[] = {
        WATCH_TEXT_ID_FATHER,
        WATCH_TEXT_ID_MOTHER,
        WATCH_TEXT_ID_AUNT,
        WATCH_TEXT_ID_UNCLE,
        WATCH_TEXT_ID_BOY,
        WATCH_TEXT_ID_GIRL
    };
    uint8_t name_cnt = sizeof(name_id) / sizeof(watchLangTxtId_t);
    ext->portrait = (uint8_t *)lv_mem_alloc(name_cnt);
    uint32_t len = 0;
    ext->max_name_len = 0;
    for(uint8_t i = 0; i < name_cnt; i++) {
        char * name = (char *)lv_lang_get_text(name_id[i]);
        uint8_t name_len = strlen(name) + 1;
        if(name_len > ext->max_name_len) ext->max_name_len = name_len;
        len += name_len;

        ext->portrait[i] = cta_phonebook_get_portrait_id(name_id[i]);
    }
    char * options = (char *)lv_mem_alloc(len);
    len = 0;
    for(uint8_t i = 0; i < name_cnt; i++) {
        char * name = (char *)lv_lang_get_text(name_id[i]);
        memcpy(&options[len], name, strlen(name));
        len += strlen(name);
        if((name_cnt - 1) == i) {
            options[len] = '\0';
        } else {
            options[len++] = '\n';
        }
    }
    lv_dropdown_set_options(ext->ddlist_name, options);
    lv_obj_align(ext->ddlist_name, label_name, LV_ALIGN_OUT_RIGHT_MID, 10, 0);
    lv_obj_set_top(cont_name, true);
    lv_mem_free(options);

    lv_coord_t interval_cont_name = 10;
    lv_obj_align(cont_name, obj, LV_ALIGN_IN_TOP_MID, 0, interval_cont_name);

    lv_obj_t * cont_number = lv_cont_create(obj, cont_name);
    lv_watch_obj_add_element(cont_number);

    lv_obj_t * label_number = lv_label_create(cont_number, NULL);
    lv_obj_add_style(label_number, LV_LABEL_PART_MAIN, &lv_watch_font20);
    lv_label_set_text_id(label_number, WATCH_TEXT_ID_NUMBER);
    lv_obj_align(label_number, cont_number, LV_ALIGN_IN_LEFT_MID, 20, 0);

    ext->ta_number = lv_textarea_create(cont_number, NULL);
    lv_obj_set_width(ext->ta_number, lv_obj_get_width(cont_number) * 2 / 3);
    lv_textarea_set_accepted_chars(ext->ta_number, "0123456789");
    lv_textarea_set_max_length(ext->ta_number, NV_CONTACTS_MAX_NUMBER_LEN - 1);
    lv_textarea_set_one_line(ext->ta_number, true);
    lv_obj_set_style_local_text_font(ext->ta_number, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, LV_THEME_WATCH_NIGHT_FONT_SMALL);
    lv_textarea_set_text(ext->ta_number, "");
    lv_obj_align(ext->ta_number, label_number, LV_ALIGN_OUT_RIGHT_MID, 10, 0);
    lv_watch_obj_add_element(ext->ta_number);

    lv_obj_align(cont_number, cont_name, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);

    lv_obj_t * btn_save = lv_btn_create(obj, NULL);
    lv_obj_set_size(btn_save, 50, 50);
    lv_obj_t * label_save = lv_label_create(btn_save, NULL);
    lv_obj_add_style(label_save, LV_LABEL_PART_MAIN, &lv_watch_font20);
    lv_label_set_text_id(label_save, WATCH_TEXT_ID_SAVE);
    lv_obj_align(btn_save, obj, LV_ALIGN_IN_BOTTOM_RIGHT, -8, -10);
    lv_obj_set_event_cb(btn_save, cta_phonebook_save_event_cb);
    lv_watch_obj_add_element(btn_save);

    lv_obj_t * cont_key = lv_cont_create(obj, NULL);
    lv_coord_t width_cont_key = lv_obj_get_width(obj) - lv_obj_get_width(btn_save) - 20;
    lv_coord_t height_cont_key = lv_obj_get_height(obj) - cont_number->coords.y2 - 10;
    lv_obj_set_size(cont_key, width_cont_key, height_cont_key);
    lv_obj_align(cont_key, obj, LV_ALIGN_IN_BOTTOM_LEFT, 8, -8);
    lv_obj_add_style(cont_key, LV_CONT_PART_MAIN, &lv_watch_style_transp);
    lv_watch_obj_add_element(cont_key);

    char * key[CTA_PHONEBOOK_KEY_NUM] = {
        "1", "2", "3", "x", "4", "5", "6", "7", "8", "9", "0"
    };
    lv_obj_t * btn_key;
    lv_obj_t * label_key;
    lv_coord_t interval_btn_key = 2;
    lv_coord_t width_btn_key = (width_cont_key - interval_btn_key * 3) / 4;
    lv_coord_t height_btn_key = (height_cont_key - interval_btn_key * 2) / 3;
    lv_coord_t x_pos = 0;
    lv_coord_t y_pos = 0;
    for(uint8_t i = 0; i < CTA_PHONEBOOK_KEY_NUM; i++) {
        btn_key = lv_btn_create(cont_key, NULL);
        lv_obj_set_size(btn_key, width_btn_key, height_btn_key);
        lv_obj_set_pos(btn_key, x_pos, y_pos);

        label_key = lv_label_create(btn_key, NULL);
        lv_obj_add_style(label_key, LV_LABEL_PART_MAIN, &lv_watch_font20);
        lv_label_set_text(label_key, key[i]);

        lv_watch_set_free_num(btn_key, key[i][0]);
        lv_obj_set_event_cb(btn_key, cta_phonebook_btn_event_cb);

        lv_watch_obj_add_element(btn_key);

        x_pos += width_btn_key + interval_btn_key;
        if((3 == i) || (6 == i)) {
            x_pos = 0;
            if(3 == i)
                y_pos = height_btn_key + interval_btn_key;
            else
                y_pos = (height_btn_key + interval_btn_key) * 2;
        }
    }

    return obj;
}

static void cta_phonebook_add_event_cb(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        cta_phonebook_add_create(NULL);
    }
}

static void cta_phonebook_del_event_cb(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        uint32_t length = sizeof(nv_watch_phonebook_t);
        nv_watch_phonebook_t * nvm_contact_list = (nv_watch_phonebook_t *)lv_mem_alloc(length);
        memset(nvm_contact_list, 0, length);
        if(length != UI_NV_Write_Req(NV_SECTION_UI_PHONEBOOK, 0, length, (uint8_t *)nvm_contact_list)) {
            printf("write nvm error in cta_phonebook_del_cb\n");
            lv_mem_free(nvm_contact_list);
        } else {
            lv_mem_free(nvm_contact_list);
            cta_phonebook_notice_create(WATCH_TEXT_ID_ALL_CONTACTS_DEL);
        }
    }
}

#endif /*USE_LV_WATCH_CTA*/
